package com.hzqiuxm.tdddemo001.ship;

import lombok.Data;

/**
 * Copyright © 2020�? tdd-demo-001. All rights reserved.
 *
 * @author 临江�? hzqiuxm@163.com
 * 船类
 * @date 2020/7/6 11:58
 */
@Data
public class Ship {

    private Location location;
    private Planet planet;



    public Ship(Location location, Planet planet) {

        this.location = location;
        this.planet = planet;
    }

    /**
     * 向前行驶
     * @return
     */
    public boolean moveForward() {

       return location.forward(planet.getMax(),planet.getObstacles());
    }

    /**
     * 向后倒退
     * @return
     */
    public boolean moveBackward() {

        return location.backward(planet.getMax(),planet.getObstacles());
    }

    /**
     * 左转弯
     */
    public void turnleft() {
         location.turnLeft();
    }

    /**
     * 右转弯
     */
    public void turnRight() {
        location.turnRight();
    }

    /**
     * 接收行动指令
     * @param commands
     */
    public String receiveCommand(String commands) {


        String result = "";

        for (char command : commands.toCharArray()) {
            boolean flag = true;
            switch (command) {
                case 'f': flag = moveForward();
                    break;

                case 'b': flag = moveBackward();
                    break;

                case 'l': turnleft();
                    break;

                case 'r': turnRight();
                    break;
            }

            if (!flag) {
                result += "X";
            }else {
                result += "O";
            }

        }

        return result;
    }
}
